package edu.umd.mhealth.lab7;

import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.oauth.OAuthService;

import android.os.AsyncTask;
import android.view.View;
import android.widget.Toast;


/* An HttpPostTask takes a request and an OAuthService and sends a POST request to passed service */
public class HttpPostTask extends AsyncTask<String, Void, Response>{
	private View view;
	private OAuthService service;
	private Token accessToken;

	public HttpPostTask(View view, OAuthService service, Token accessToken){
		this.view = view;
		this.service = service;
		this.accessToken = accessToken;
	}

	@Override
	protected Response doInBackground(String... params) {
		// create, sign and send the passed request
		OAuthRequest request = new OAuthRequest(Verb.POST, params[0]);
		service.signRequest(accessToken, request);
		return request.send();
	}

	protected void onPostExecute(Response response){
		if (response.getCode() == 201){
			Toast.makeText(view.getContext(), "Sent Successfully.", 5).show();
		} else {
			Toast.makeText(view.getContext(), "Send Failed.", 8).show();
		}
	}
}
